$(document).ready(function() {

        reload_group_form = function(group_id,elem)
        {
            var url = base_url+'group_attributes/get_html_on_group/'+group_id+'/'+myObject['mode']+'/'+myObject['id'];
            $.ajax({
                    url: url,
                    dataType: 'json',
                    type: "POST",
                    success: function (data, textStatus, xhr) {

                        $(elem).parent().parent().find('.data-container').html(data.group_html)

                        $(formbutton).removeAttr("disabled");

                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                    }
            });  
        }

        view_attributes=function (group_id,elem)
        {
            var url = base_url+'group_attributes/get_attributes_on_group/'+group_id;
            var dialogClass = 'view_edit_attributes_on_group';
            var dialogTitle = 'Edit Attributes';
            var formToSubmit = '#attributes_on_group';
            var buttons = null; 
            openNewDialogOnOpen = function() {

                // Return a helper with preserved width of cells
                var fixHelper = function(e, ui) {
                    ui.children().each(function() {
                            $(this).width($(this).width());
                    });
                    return ui; 
                };

                //sort object attributes
                $("#sortable tbody").sortable({
                        helper: fixHelper,
                        placeholder: "ui-state-highlight",
                        axis: 'y',
                        handle: ".move_post",
                        update: function(event,ui)
                        {

                            $.ajax({
                                    url: $(formToSubmit).attr('action'),
                                    dataType: 'json',
                                    type: "POST",                              
                                    data: $(formToSubmit).serialize(),
                                    success: function (data, textStatus, xhr) {

                                        // Fill form  with updated fileds
                                        reload_group_form(group_id,elem)

                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                                    }
                            });

                        }
                }).disableSelection();

                if(!myObject['hasDeleteHandler'])
                    {
                    $('.delete_group_attribute').live('click',function(){
                            data_holder = $(this).attr('data-holder');
                            var data=data_holder.split("_");

                            group_id = data[0];
                            attribute_id = data[1];

                            var url = base_url+'group_attributes/set_active_group_attribute/'+group_id+'/'+attribute_id;
                            var elem = this;

                            $.ajax({
                                    url: url,
                                    dataType: 'json',
                                    type: "POST",
                                    success: function (data, textStatus, xhr) {

                                        $(elem).parent().parent().remove();

                                        // Fill form  with updated fileds
                                        reload_group_form(group_id,myObject['groupReference']);

                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                                    }
                            });  
                            return false;

                    });
                    myObject['hasDeleteHandler']=true;
                }
            }
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,1110,$(window).height()-40)
        }


        $('.view_attributes_on_group').live('click',function(){
                group_id = $(this).attr('data-holder');
                if(group_id=='')
                    {
                    alert('Select type first')
                }else
                    {
                    myObject['groupReference'] = this;
                    view_attributes(group_id,this);   
                }
        })




})